
# 捕获异常
a = -1
try:
    a = 1 / 4
except Exception as e:
    print(f'Error:{e}')
else:
    print(f'Success')
finally:
    print('必定执行的代码')

print(f'a:{a}')



# 抛出异常
b = 1.53
if b > 2:
    raise ValueError('数据错误')
else:
    print(f'数据正确 b={b}')



# 自定义异常
class ServiceException(Exception):
    
    def __init__(self, *args):
        super().__init__(*args)

c = 15
if c % 3 != 0:
    raise ServiceException('不能被3整除')
else:
    print('可以被3整除')



# 断言
def devide(m, n):
    assert n != 0
    return m / n

d = devide(12, 3)
print(f'd = {d}')